package com.train.course.service.impl;

import com.train.course.domain.VideoComment;
import com.train.course.dto.CommentPublishDTO;
import com.train.course.mapper.VideoCommentMapper;
import com.train.course.service.IVideoCommentService;
import com.train.course.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 视频评论 服务层实现
 */
@Service
public class VideoCommentServiceImpl implements IVideoCommentService {

    @Autowired
    private VideoCommentMapper videoCommentMapper;

    /**
     * 发布评论
     */
    @Override
    @Transactional
    public boolean publishComment(Long userId, Long courseId, CommentPublishDTO dto) {
        VideoComment videoComment = new VideoComment();
        videoComment.setUserId(userId);
        videoComment.setVideoId(courseId);
        videoComment.setVideoType("1"); // 视频集类型
        videoComment.setParentId(dto.getParentId() != null ? dto.getParentId() : 0L);
        videoComment.setContent(dto.getContent());
        videoComment.setLikeCount(0L);
        videoComment.setStatus("0"); // 正常状态
        videoComment.setCreateBy(String.valueOf(userId));

        int result = videoCommentMapper.insertVideoComment(videoComment);
        return result > 0;
    }

    /**
     * 查询主评论列表（下滑刷新）
     */
    @Override
    public List<CommentVO> getMainComments(Long courseId, Long lastCommentId, Integer pageSize) {
        return videoCommentMapper.selectMainCommentsWithUser(courseId, lastCommentId, pageSize);
    }

    /**
     * 查询子评论列表（下滑刷新）
     */
    @Override
    public List<CommentVO> getReplies(Long parentId, Long lastReplyId, Integer pageSize) {
        return videoCommentMapper.selectRepliesWithUser(parentId, lastReplyId, pageSize);
    }
}
